/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： AppService.java
 * Date：18-11-5 上午10:54
 * Author: guomw
 */

package com.guomw.report.service.service.upgrade;

import com.guomw.report.common.config.Constant;
import com.guomw.report.service.entity.SystemConfig;
import com.guomw.report.service.enums.DBVersion;
import com.guomw.report.service.repository.system.SystemConfigRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.sql.Statement;
import java.util.List;

/**
 * @author guomw
 * @date 2018/9/11
 */
@Service
public class AppService implements ApplicationListener<ContextRefreshedEvent> {
    private static Log log = LogFactory.getLog(AppService.class);
    @Autowired
    private SystemConfigRepository systemConfigRepository;
    @Autowired
    private Environment env;
    @Autowired
    private BaseService baseService;
    @Autowired
    private JdbcService jdbcService;

    private static String systemStringVersionKey="DatabaseVersion";

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            SystemConfig systemConfig = systemConfigRepository.findOne(systemStringVersionKey);
            if (systemConfig == null) {
                SystemConfig databaseVersion = new SystemConfig();
                databaseVersion.setCode(systemStringVersionKey);
                databaseVersion.setValueForCode(String.valueOf(DBVersion.initVersion.ordinal()));
                databaseVersion.setRemark(DBVersion.initVersion.getVersion());
                systemConfigRepository.save(databaseVersion);
            }
            if (!env.acceptsProfiles(Constant.PROFILE_UNIT_TEST)) {
                baseService.systemUpgrade(systemStringVersionKey,DBVersion.class,DBVersion.initVersion,upgrade->{
                    switch(upgrade){
                        case initVersion:
                            break;
                        default:
                            break;
                    }
                });
            }
        }
    }


    private void runJdbcWork(List<String> listSql, DBVersion commonVersion) {
        try {
            if (listSql == null || listSql.size() == 0) {
                return;
            }
            for (String hql : listSql) {
                runJdbcWork(hql, commonVersion);
            }
        } catch (Exception e) {
            log.error("update to" + commonVersion.getVersion() + " error", e);
        }
    }


    private void runJdbcWork(String hql, DBVersion commonVersion) {
        try {
            jdbcService.runJdbcWork(connection -> {
                Statement statement = connection.getConnection().createStatement();
                statement.execute(hql);
            });
        } catch (Exception e) {
            log.error("update to" + commonVersion.getVersion() + ",sql[" + hql + "] error", e);
        }
    }


}
